//package com.example.demo.multidatasource;
//
//import com.example.demo.util.HibernatePropertiesUtil;
//import com.example.demo.util.SpringContextUtil;
//import org.hibernate.*;
//import org.hibernate.boot.spi.SessionFactoryOptions;
//import org.hibernate.engine.spi.FilterDefinition;
//import org.hibernate.metadata.ClassMetadata;
//import org.hibernate.metadata.CollectionMetadata;
//import org.hibernate.stat.Statistics;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
//import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
//
//import javax.naming.NamingException;
//import javax.naming.Reference;
//import javax.sql.DataSource;
//import java.sql.Connection;
//import java.util.*;
//import java.util.Map.Entry;
//
//
///**
// * 此类用于创建SessionFactory
// *
// * @author Jack
// * @version V1.0
// * @Description: 此类用于创建SessionFactory，创建好的SessionFactory保存在sessionFactories中
// * @date 2017/10/24 14:42
// */
//public class MultiSessionFactory extends LocalSessionFactoryBean implements SessionFactory {
//    private final Logger logger = LoggerFactory.getLogger(this.getClass());
//
//
//    // 这个暂时还没用到，用于拦截sql
//    private Interceptor entityInterceptor;
//
//    private static Map<Long, SessionFactory> sessionFactories;
//
//    private static MultiSessionFactory multiSessionFactory = new MultiSessionFactory();
//
//    private MultiSessionFactory() {
//        sessionFactories = new HashMap<>();
//    }
//
//    public static MultiSessionFactory getInstance() {
//        return multiSessionFactory;
//    }
//
//    public Set<Long> getSessionFactoryKeys() {
//        return sessionFactories.keySet();
//    }
//
//
//    public void setEntityInterceptor(Interceptor entityInterceptor) {
//        this.entityInterceptor = entityInterceptor;
//    }
//
//
//    public SessionFactory getSessionFactory() {
//        this.logger.info("Get SessionFactory...");
//        Long dataSourceKey = this.getDataSourceKey();
//        return this.getSessionFactory(dataSourceKey);
//
//    }
//
//
//    public SessionFactory getSessionFactory(Long dataSourceKey) {
//        SessionFactory sessionFactory = sessionFactories.get(dataSourceKey);
//        this.logger.info("get sessionFactory<data source key :" + dataSourceKey + "> ");
//        if (null != sessionFactory) {
//            return sessionFactory;
//        }
//        sessionFactory = buildSessionFactoryByDataSourceKey();
//        if (null != sessionFactory) {
//            return sessionFactory;
//        }
//        throw new RuntimeException("获取SessionFactory失败");
//    }
//
//    private Long getDataSourceKey() {
//        return DynamicDataSourceContextHolder.getDataSourceKey();
//    }
//
//    /**
//     * 创建多个SessionFactory
//     *
//     * @return SessionFactory
//     * @throws
//     * @author Jack
//     * @date 2017/10/24
//     */
//    @Override
//    protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
//        Set<Entry<Long, DataSource>> dsEntrySet = MultiDataSource.getInstance().getDataSourceSet();
//        Iterator iterator = dsEntrySet.iterator();
//        while (iterator.hasNext()) {
//            Entry<Long, DataSource> entry = (Entry) iterator.next();
//            Long dataSourceKey = entry.getKey();
//            DataSource ds = entry.getValue();
//            if (!sessionFactories.containsKey(dataSourceKey)) {
//                this.newSessionFactory(dataSourceKey, ds, sfb);
//            }
//        }
//        return MultiSessionFactory.getInstance();
//    }
//
//    private SessionFactory newSessionFactory(Long dataSourceKey, DataSource ds,
//                                             LocalSessionFactoryBuilder sfb) {
//        this.setDataSource(ds);
//        HibernatePropertiesUtil hibernatePropertiesUtil = SpringContextUtil.getBean
//                ("hibernatePropertiesUtil");
//        Properties properties = new Properties();
//        Map<String, String> map = hibernatePropertiesUtil.getProperties();
//        properties.putAll(map);
//
//        sfb.addProperties(properties);
//        sfb.scanPackages(map.get("hibernate.packages_to_scan"));
//        if (this.entityInterceptor != null) {
//            super.setEntityInterceptor(this.entityInterceptor);
//        }
//        SessionFactory sessionFactory = super.buildSessionFactory(sfb);
//        this.putSessionFactory(dataSourceKey, sessionFactory);
//        return sessionFactory;
//    }
//
//    /**
//     * 创建单个SessionFactory
//     *
//     * @return SessionFactory
//     * @throws
//     * @author Jack
//     * @date 2017/10/24
//     */
//    public SessionFactory buildSessionFactoryByDataSourceKey() {
//        Long dataSourceKey = this.getDataSourceKey();
//        DataSource ds = MultiDataSource.getInstance().getDataSource();
//        if (ds != null) {
//            LocalSessionFactoryBuilder sfb = new LocalSessionFactoryBuilder(ds, new
//                    PathMatchingResourcePatternResolver());
//            return this.newSessionFactory(dataSourceKey, ds, sfb);
//        } else {
//            this.logger.error("Please check whether the company is valid or not!!!!!!!!!!");
//            return null;
//        }
//    }
//
//    private void putSessionFactory(Long key, SessionFactory sessionFactory) {
//        sessionFactories.put(key, sessionFactory);
//    }
//
//    @Override
//    public void destroy() throws HibernateException {
//        Set<Long> keys = MultiSessionFactory.getInstance().getSessionFactoryKeys();
//        Iterator iterator = keys.iterator();
//        while (iterator.hasNext()) {
//            Long key = (Long) iterator.next();
//            SessionFactory sessionFactory = MultiSessionFactory.getInstance().getSessionFactory
//                    ();
//            sessionFactory.close();
//            MultiDataSource.getInstance().removeDataSource(key);
//        }
//    }
//
//
//    @Override
//    public SessionFactoryOptions getSessionFactoryOptions() {
//        return this.getSessionFactory().getSessionFactoryOptions();
//    }
//
//    @Override
//    public SessionBuilder withOptions() {
//        return this.getSessionFactory().withOptions();
//    }
//
//    @Override
//    public Session openSession() throws HibernateException {
//        return this.getSessionFactory().openSession();
//    }
//
//    @Override
//    public Session getCurrentSession() throws HibernateException {
//        return this.getSessionFactory().getCurrentSession();
//    }
//
//    @Override
//    public StatelessSessionBuilder withStatelessOptions() {
//        return this.getSessionFactory().withStatelessOptions();
//    }
//
//    @Override
//    public StatelessSession openStatelessSession() {
//        return this.getSessionFactory().openStatelessSession();
//    }
//
//    @Override
//    public StatelessSession openStatelessSession(Connection connection) {
//        return this.getSessionFactory().openStatelessSession(connection);
//    }
//
//    @Override
//    public ClassMetadata getClassMetadata(Class aClass) {
//        return this.getSessionFactory().getClassMetadata(aClass);
//    }
//
//    @Override
//    public ClassMetadata getClassMetadata(String s) {
//        return this.getSessionFactory().getClassMetadata(s);
//    }
//
//    @Override
//    public CollectionMetadata getCollectionMetadata(String s) {
//        return this.getSessionFactory().getCollectionMetadata(s);
//    }
//
//    @Override
//    public Map<String, ClassMetadata> getAllClassMetadata() {
//        return this.getSessionFactory().getAllClassMetadata();
//    }
//
//    @Override
//    public Map getAllCollectionMetadata() {
//        return this.getSessionFactory().getAllClassMetadata();
//    }
//
//    @Override
//    public Statistics getStatistics() {
//        return this.getSessionFactory().getStatistics();
//    }
//
//    @Override
//    public void close() throws HibernateException {
//        Set<Map.Entry<Long, SessionFactory>> factoryEntries = sessionFactories.entrySet();
//        Iterator iterator = factoryEntries.iterator();
//        while (iterator.hasNext()) {
//            Map.Entry<Long, SessionFactory> entry = (Map.Entry) iterator.next();
//            SessionFactory sessionFactory = entry.getValue();
//            sessionFactory.close();
//        }
//    }
//
//    @Override
//    public boolean isClosed() {
//        return this.getSessionFactory().isClosed();
//    }
//
//    @Override
//    public Cache getCache() {
//        return this.getSessionFactory().getCache();
//    }
//
//    @Override
//    public Set getDefinedFilterNames() {
//        return this.getSessionFactory().getDefinedFilterNames();
//    }
//
//    @Override
//    public FilterDefinition getFilterDefinition(String s) throws HibernateException {
//        return this.getSessionFactory().getFilterDefinition(s);
//    }
//
//    @Override
//    public boolean containsFetchProfileDefinition(String s) {
//        return this.getSessionFactory().containsFetchProfileDefinition(s);
//    }
//
//    @Override
//    public TypeHelper getTypeHelper() {
//        return this.getSessionFactory().getTypeHelper();
//    }
//
//    @Override
//    public Reference getReference() throws NamingException {
//        return this.getSessionFactory().getReference();
//    }
//}
